<template>
  <div class="guns-body">
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="工单名称:">
              <a-input
                v-model:value.trim="where.remarks"
                placeholder="请输入工单名称"
                allow-clear
              />
            </a-form-item>
            <a-form-item label="处理人员">
              <a-input
                v-model:value.trim="where.processor"
                placeholder="请输入处理人员"
                allow-clear
              />
            </a-form-item>
          </a-row>
          <a-row>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false" class="table-height">
        <BasicTable
            :canResize="false"
            ref="tableRef"
            :api="OaTaskApi.findPage"
            :where="where"
            :columns="columns"
            showTableSetting
            rowKey="id"
            :rowSelection="{
                type:'checkbox',
                selectedRowKeys: checkedKeys,
                onChange: onSelectChange,
            }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <div class="table-toolbar">
              <a-space>
                <a-button type="primary" @click="openEdit()">
                  <template #icon>
                    <plus-outlined />
                  </template>
                  <span>新建</span>
                </a-button>
              </a-space>
            </div>
          </template>

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="guns-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <OaTaskEdit v-model:visible="showEdit" v-if="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script  lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { message } from 'ant-design-vue';

import OaTaskEdit from './oa-task-edit.vue';
import { OaTaskApi } from '/@/api/oaTask/OaTaskApi.ts';

  // 查询条件
  const where = reactive({
  });


    // 多选选中列表
    const checkedKeys = ref<Array<string | number>>([]);

    // 当前行数据
    const current = ref<any>(null);

    // 是否显示新增编辑弹框
    const showEdit = ref<boolean>(false);


    // 表格配置
    const columns = ref<any[]>([

        {
          title: '工单编号',
          dataIndex: 'taskNumber'
        },
        {
          title: '工单名称',
          dataIndex: 'remarks'
        },
        {
          title: '工单类型',
          dataIndex: 'taskType',
          customRender: function ({ text }) {
            if ( 0 === text) {
              return '普通工单';
            }
            if ( 1 === text) {
              return '紧急工单';
            }
            if ( 2 === text) {
              return '重要工单';
            }
            return "---";
          },
        },
        {
          title: '工单状态',
          dataIndex: 'taskStatus',
          customRender: function ({ text }) {
            if ( 0 === text) {
              return '待处理';
            }
            if ( 1 === text) {
              return '处理中';
            }
            if ( 2 === text) {
              return '已完成';
            }
            if ( 3 === text) {
              return '已关闭';
            }
            return "---";
          },
        },
        {
          title: '优先级别',
          dataIndex: 'priority',
          customRender: function ({ text }) {
            if ( 0 === text) {
              return '低';
            }
            if ( 1 === text) {
              return '中';
            }
            if ( 2 === text) {
              return '高';
            }
            return "---";
          },
        },
        {
          title: '处理人员',
          dataIndex: 'processor'
        },
        {
          title: '截止时间',
          dataIndex: 'deadline'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
    ])

  // ref
  const tableRef = ref(null);

  /**
   * 点击查询
   * @author: nxy
   * @Date: 2022-10-12 09:38:18
   */
   const reload = () => {
    checkedKeys.value = [];
    tableRef?.value.reload({ page: 1 });
  };

  /**
   * 点击重置
   * @author: nxy
   * @Date: 2022-10-12 09:38:29
   */
  const reset = () => {
    where.remarks = '';
    where.processor = '';
    reload();
  };

  // 表格选中改变
  const onSelectChange = (selectedRowKeys: (string | number)[]) => {
    checkedKeys.value = selectedRowKeys;
  };


  /**
   * 删除单个
   *
   * @author chenjinlong
   * @date 2021/4/2 17:03
   */
   const remove = async (row: any) => {
    const result = await OaTaskApi.delete({ id: row.id });
    message.success(result.message);
    reload();
  };

  /**
   * 打开编辑弹窗
   *
   * @author chenjinlong
   * @date 2021/4/2 17:03
   */
  const openEdit = (row?: any) => {
    current.value = row;
    showEdit.value = true;
  };
</script>
